/*
 * To change MyLogger license header, choose License Headers in Project Properties.
 * To change MyLogger template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ice.proxy.log;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author ice
 */
public class MyLogger {
    
    private static final Map<Class<?>, Logger> loggerClassMap = new ConcurrentHashMap<>();
    private static final Map<String, Logger> loggerStringMap = new ConcurrentHashMap<>();
    
    private static final ObjectMapper mapper = new ObjectMapper();
    
    public static void trace(Object target, String msg, Object data, Exception exception) {
        Logger logger = getLogger(target);
        if (exception == null) {
            logger.trace(format(msg, data));
        } else {
            logger.trace(format(msg, data), exception);
        }
    }
    
    public static void debug(Object target, String msg, Object data, Exception exception) {
        Logger logger = getLogger(target);
        if (exception == null) {
            logger.debug(format(msg, data));
        } else {
            logger.debug(format(msg, data), exception);
        }
    }
    
    public static void info(Object target, String msg, Object data, Exception exception) {
        Logger logger = getLogger(target);
        if (exception == null) {
            logger.info(format(msg, data));
        } else {
            logger.info(format(msg, data), exception);
        }
    }
    
    public static void warn(Object target, String msg, Object data, Exception exception) {
        Logger logger = getLogger(target);
        if (exception == null) {
            logger.warn(format(msg, data));
        } else {
            logger.warn(format(msg, data), exception);
        }
    }
    
    public static void error(Object target, String msg, Object data, Exception exception) {
        Logger logger = getLogger(target);
        if (exception == null) {
            logger.error(format(msg, data));
        } else {
            logger.error(format(msg, data), exception);
        }
    }
    
    private static Logger getLogger(Object target) {
        Logger localLogger = null;
        if (target instanceof Class<?>) {
            Class<?> targetClass = (Class<?>) target;
            if (null == loggerClassMap.get(targetClass)) {
                localLogger = LoggerFactory.getLogger(targetClass);
                loggerClassMap.put(targetClass, localLogger);
            }
            localLogger = loggerClassMap.get(targetClass);
        } else if (target instanceof String) {
            String targetName = (String) target;
            if (null == loggerStringMap.get(targetName)) {
                localLogger = LoggerFactory.getLogger(targetName);
                loggerStringMap.put(targetName, localLogger);
            }
            localLogger = loggerStringMap.get(targetName);
        }
        if (null == localLogger) {
            if (null == loggerStringMap.get("default")) {
                localLogger = LoggerFactory.getLogger("default");
            }
            loggerStringMap.put("default", localLogger);
            localLogger = loggerStringMap.get("default");
        }
        return localLogger;
    }
    
    private static String format(String msg, Object data) {
        String format = null;
        try {
            format = String.format("[%s] [%s]", msg, mapper.writeValueAsString(data));
        } catch (JsonProcessingException ex) {
            getLogger("default").error("json转换出现问题", ex);
        }
        return format;
    }
}
